package service.impl;

import java.util.List;

import dao.IBanjiDao;
import dao.impl.BanjiDaoImpl;
import entity.Banji;
import entity.Banji_PageBean;
import entity.Student;
import service.IBanjiService;

public class BanjiServiceImpl implements IBanjiService{
	private IBanjiDao banjiDao = new BanjiDaoImpl();

	@Override
	public List<Banji> search(String searchName) {
		return banjiDao.search(searchName);
	}

	@Override
	public boolean insert(Banji banji) {
		List<Banji> list =  banjiDao.search(banji.getName());
		if (list.size() > 0) {
			System.out.println("该用户已经存在");
			// 该名字用户已经存在
			return true;
		} else {
			int count = banjiDao.insert(banji);
			return count == 1 ? true : false;
			}
	}

	@Override
	public boolean update(Banji banji) {
		int count = banjiDao.update(banji);
		return count == 1 ? true : false;
	}

	@Override
	public boolean deleteById(Integer id) {
		int count = banjiDao.deleteById(id);
		return count == 1 ? true : false;
	}

	@Override
	public Banji_PageBean pageList(Integer pageNo, Integer pageSize) {
		Banji_PageBean pageBean = new Banji_PageBean();
		pageBean.setPageNo(pageNo);
		pageBean.setPageSize(pageSize);
		int count = banjiDao.getTotalCount();
		int totalPage = (int) Math.ceil((double)count / pageSize);
		pageBean.setTotalPage(totalPage);

		int offset = (pageNo - 1) * pageSize;
		List<Banji> list = banjiDao.pageList(offset, pageSize);
		pageBean.setList(list);
		return pageBean;
	}

	@Override
	public Banji selectById(int id) {
		return banjiDao.selectById(id);
	}

}
